package com.kunbo.controller;

import com.alibaba.fastjson.JSON;
import com.kunbo.entity.Bearing;
import com.kunbo.entity.LineChart;
import com.kunbo.service.BearingService;
import com.kunbo.service.FFTServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/fft")
@CrossOrigin(origins = "${kunbo.web.path}" ,allowedHeaders = "*", allowCredentials = "true")
public class FFTController {

    @Autowired
    private FFTServer fftServer;
    @Autowired
    private BearingService bearingService;

    @RequestMapping(value = "/getxfft",method = RequestMethod.GET)
    @ResponseBody
    public String getAll() {
        List<Bearing> x = bearingService.getAll();//取到x的list
        List<Double> doubles = fftServer.runFFTForX();
        List<Double> frequency = fftServer.frequency();
//放入时间和xyz数据
        List list = new ArrayList();
        List list2 = new ArrayList();
//        List list3 = new ArrayList();
//        List list4 = new ArrayList();
        //获取x的频谱值
            for(int i = 0; i < doubles.size()/2; i++){
                list2.add(doubles.get(i));
            }
//        for (double d : doubles) {
//            list2.add(d);
//        }
            for(int i = 0; i < frequency.size()/2; i++){
                list.add(frequency.get(i));
            }
//        for (double f : frequency) {
//            list.add(f);
//        }

        for (Bearing bearing : x) {
//            list.add(bearing.getTime());
//            list2.add(bearing.getX());
//            list3.add(bearing.getY());
//            list4.add(bearing.getZ());
        }
        //实体类
        LineChart lineChart = new LineChart();
        //实体类的内部类
        LineChart.time time = lineChart.new time();
        LineChart.data data = lineChart.new data();
        LineChart.data data1 = lineChart.new data();
        LineChart.data data2 = lineChart.new data();
        LineChart.type type1 = lineChart.new type();
        LineChart.type type2 = lineChart.new type();
        LineChart.type type3 = lineChart.new type();
        LineChart.realDate realDate = lineChart.new realDate();
        LineChart.realDate realDate2 = lineChart.new realDate();
        LineChart.realDate realDate3 = lineChart.new realDate();
        //存入“common“
        if(time.getMonth() == null){
            time.setMonth(list);
            lineChart.setCommon(time);
        }
        List<LineChart.realDate> list1 = new ArrayList<>();
        List<LineChart.realDate> list5 = new ArrayList<>();
        List<LineChart.realDate> list6 = new ArrayList<>();

        realDate.setName("xfft");
        realDate.setData(list2);
        realDate2.setName("y");
//        realDate2.setData(list3);
        realDate3.setName("z");
//        realDate3.setData(list4);
        list1.add(realDate);
        list5.add(realDate2);
        list6.add(realDate3);

        data.setTitle("FFTx");
        data.setData(list1);
        data1.setTitle("振动数据y");
        data1.setData(list5);
        data2.setTitle("振动数据z");
        data2.setData(list6);
        type1.setKey("map");
        type1.setText("FFTX");
        type2.setKey("seller");
        type2.setText("振动数据y");
        type3.setKey("commodity");
        type3.setText("振动数据z");
        List<LineChart.type> type = new ArrayList<>();
        type.add(type1);
        type.add(type2);
        type.add(type3);
        lineChart.setType(type);
        lineChart.setMap(data);
        lineChart.setSeller(data1);
        lineChart.setCommodity(data2);
        String s = JSON.toJSONString(lineChart);
        return s;
    }

}
